package com.health.viewscontrols.medical.device.ecg;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.health.httpbean.model.EcgModel;
import com.health.viewscontrols.medical.PhotoViewActivity;
import com.health.viewscontrols.medical.base.BaseDeviceActivity;
import com.novabluetooth.data.DeviceShowSaveData;
import com.novabluetooth.services.hqxd.HQXDActivity;
import com.novabluetooth.services.libecg.ecgdemo.MainPC80B;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ECGLKActivity extends BaseDeviceActivity {

    private String imgUrl;

    @Override
    public void startDevice() {

        showSelectEcg();
//        startActivityForResult(new Intent(this, MainPC80B.class),0x1001);
    }

    @Override
    public void disconnectDevice() {

    }

    @Override
    public void doBusiness() {
        tv_bind_device.setVisibility(View.INVISIBLE);
        tv_operation_tips.setText(com.nova.health.ble.R.string.bf_ecg);
    }

    @Override
    public void update(String hexStr) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 0x1001) {
                String heartRate = data.getStringExtra("heartRate");
                String hrResult = data.getStringExtra("hrResult");
                String ecgImgUrl = data.getStringExtra("ecgImgUrl");
                if (!TextUtils.isEmpty(heartRate)) {
                    Log.e(TAG, "onActivityResult: " + heartRate + hrResult);
                    tv_result.setText("测量结果：心率 " + heartRate + "\n" + "" + hrResult);

                    EcgModel ecgModel = new EcgModel();
                    ecgModel.setHr(heartRate + "");
                    ecgModel.setEcgResult(hrResult);
                    ecgModel.setEcgImg(ecgImgUrl);
                    Log.e(TAG, "onActivityResult: " + ecgModel.toString());
                    DeviceShowSaveData.saveEcg(GsonUtils.toJson(ecgModel));
                    if (!TextUtils.isEmpty(ecgImgUrl)) {
                        try {
                            Bitmap bitmap = getLocalBitmap(ecgImgUrl);
                            show_img.setImageBitmap(bitmap);
                            show_img.setVisibility(View.VISIBLE);
                            showPhotoView(ecgImgUrl);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    Log.e(TAG, "onActivityResult: " + heartRate + hrResult);
                    tv_result.setText("未测量，请测量心电");
                }

            }
        }
    }


    public void showSelectEcg() {
        final String[] items = new String[]{"华清心电", "力康快速检测心电"};
        new AlertDialog.Builder(ECGLKActivity.this).setTitle("选择心电设备")
                .setItems(items, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) {
                            //华清心电
                            startActivityForResult(new Intent(ECGLKActivity.this, HQXDActivity.class), 0x1001);
                        } else if (which == 1) {
                            //力康快速检测心电
                            startActivityForResult(new Intent(ECGLKActivity.this, MainPC80B.class), 0x1001);
                        }
                    }
                }).create().show();
    }

    public Bitmap getLocalBitmap(String url) {

        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void showPhotoView(String ecgImgUrl) {
        if (!TextUtils.isEmpty(ecgImgUrl)) {
            show_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(ECGLKActivity.this, PhotoViewActivity.class)
                            .putExtra("imgUrl", ecgImgUrl));
                }
            });
        } else {
            ToastUtils.showShort("图片不能为空");
        }
    }
}
